package com.kangaroo.myhome.service.impl;

import java.util.*;

import com.kangaroo.myhome.model.ModdifyPasword;
import com.kangaroo.myhome.model.TSysUserExample;
import com.kangaroo.myhome.model.TSysUserSearch;
import com.kangaroo.myhome.utils.SecurityUtils;
import com.kangaroo.myhome.vo.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.kangaroo.myhome.mapper.TSysUserMapper;
import com.kangaroo.myhome.model.TSysUser;
import tk.mybatis.mapper.util.StringUtil;

@Service
public class TSSysUserService {

    @Autowired
    private TSysUserMapper tSysUserMapper;

    @Autowired
    private SysUserServiceImpl userService;

    @Value("${user.init.password}")
    private String initPassword;

    /**
     * @param account
     * @param username
     * @return
     */
    public ResponseModel findAll(String account, String username, int page, int rows) {
        ResponseModel model = new ResponseModel();

        TSysUserSearch user = new TSysUserSearch();
        user.setAccount(account);
        user.setUserName(username);
        user.setPagination(page, rows);
        List<TSysUser> users = tSysUserMapper.selectByPagination(user);
        long count = tSysUserMapper.selectByPaginationCount(user);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(users, count);
        return model;
    }

    /**
     * @param tSysUser
     * @return
     */
    public ResponseModel edit(TSysUser tSysUser) {

        ResponseModel model = new ResponseModel();

        if (tSysUser == null) {
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }

        int result = 0;
        String id = tSysUser.getId();
        if (StringUtil.isEmpty(id)) {
            TSysUser userExist = view(tSysUser);
            if (userExist != null) {
                model.setCode(201);
                model.setMessage("工号：【" + tSysUser.getAccount() + "】已经存在！");
                return model;
            } else {
                tSysUser.setCreateTime(new Date());
                tSysUser.setPassword(new BCryptPasswordEncoder().encode(initPassword));
                tSysUser.setId(UUID.randomUUID().toString());
                tSysUser.setCreator(SecurityUtils.getUsername());
                result = tSysUserMapper.insertSelective(tSysUser);
            }

        } else {
            result = tSysUserMapper.updateByPrimaryKeySelective(tSysUser);
        }

        model.setCode(200);
        model.setMessage("操作成功");
        return model;
    }

    /**
     * @param tSysUser
     * @return
     */
    public TSysUser view(TSysUser tSysUser) {
        ResponseModel model = new ResponseModel();

        TSysUserExample example = new TSysUserExample();
        String id = tSysUser.getId();
        String account = tSysUser.getAccount();
        TSysUser resultUser = null;
        if (StringUtil.isNotEmpty(id)) {
            // example.createCriteria().andAccountLike(id);
            resultUser = tSysUserMapper.selectByPrimaryKey(id);
            return resultUser;
        }
        if (StringUtil.isNotEmpty(account)) {
            example.createCriteria().andAccountEqualTo(account);
            List<TSysUser> users = tSysUserMapper.selectByExample(example);
            if (users != null && users.size() > 0) {
                resultUser = users.get(0);
            }
        }

        return resultUser;
    }

    public void delete(TSysUser tSysUser) {
        tSysUser.setState("1");
        tSysUserMapper.updateByPrimaryKeySelective(tSysUser);
    }

    /**
     * @param code
     * @return
     */
    public TSysUser selectUserByCode(String code) {

        ResponseModel model = new ResponseModel();

        TSysUserExample userExample = new TSysUserExample();
        userExample.createCriteria().andAccountEqualTo(code);

        List<TSysUser> users = tSysUserMapper.selectByExample(userExample);

        return users.get(0);
    }

    public ResponseModel modifyPassword(ModdifyPasword moddifyPasword) {
        ResponseModel model = new ResponseModel();
        if(moddifyPasword.compareTwicePasswordIsEquals()){
            model.setCode(201);
            model.setMessage("新旧密码一致，请修改！");
            return model;
        }else{
            TSysUser user = userService.findByUsername(moddifyPasword.getAccount());
            if(user==null){
                model.setCode(201);
                model.setMessage("用户名不正确！");
                return model;
            }else{
                String passwordDB = user.getPassword();
                if(!moddifyPasword.compareOldAndDBPasswordIsEquals(passwordDB)){
                    model.setCode(201);
                    model.setMessage("密码错误，请从新输入！");
                    return model;
                }else{

                    TSysUser moddifyUser = new TSysUser();
                    moddifyUser.setPassword(new BCryptPasswordEncoder().encode(moddifyPasword.getRePassword()));
                    TSysUserExample tSysUserExample = new TSysUserExample();
                    tSysUserExample.createCriteria().andAccountEqualTo(moddifyPasword.getAccount());
                    int moddifySuccessNum = tSysUserMapper.updateByExampleSelective(moddifyUser,tSysUserExample);

                    if(moddifySuccessNum>0){
                        model.setCode(200);
                        model.setMessage("密码修改成功");
                        return model;
                    }else{
                        model.setCode(201);
                        model.setMessage("密码修改失败");
                        return model;
                    }

                }
            }
        }
    }

}
